package org.anupta.game.entity.entities;

import java.util.Map;
import java.util.TreeMap;

import org.anupta.game.entity.dao.DAO;

public class Account extends Entity{
	private int guid = 0;
	private String name = null;
	private String pseudo = null;
	private String answer = null;
	private int gmLevel = 0;
	private Map<Integer,Player> players = new TreeMap<Integer,Player>();
	
    public Account(int id,String name,String pseudo,String answer,int gmLevel,String players){
    	setGuid(id);
    	setName(name);
    	setPseudo(pseudo);
    	setGmLevel(gmLevel);
    	setPlayers(players);
    }

	@Override
	public int getGuid() {
		return guid;
	}

	@Override
	public void setGuid(int guid) {
		this.guid = guid;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	public String getPseudo() {
		return pseudo;
	}

	public void setPseudo(String pseudo) {
		this.pseudo = pseudo;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public int getGmLevel() {
		return gmLevel;
	}

	public void setGmLevel(int gmLevel) {
		this.gmLevel = gmLevel;
	}
	
	public boolean isAdmin(){
		return (gmLevel == 5);
	}
	
	public Map<Integer,Player> getPlayers() {
		return players;
	}
	
	public String getPlayersString(){
		StringBuilder builder = new StringBuilder();
		for(Player player : players.values()){
			if(builder.length() > 0){
				builder.append(";");
			}
			builder.append(player.getGuid());
		}
		return builder.toString();
	}

	public void setPlayers(Map<Integer,Player> players) {
		this.players = players;
	}
	
	public void setPlayers(String playersId){
		if(playersId.isEmpty()){
			return;
		}else if(!playersId.isEmpty() && !playersId.contains(";")){
			playersId += ";";
		}
		for(String id : playersId.split(";")){
			Player player = DAO.getDao(Player.class).find(Integer.parseInt(id));
			if(player != null){
				players.put(player.getGuid(), player);
			}
		}
	}
	
	public void delPlayer(Player player){
		if(players.containsKey(player.getGuid())){
			players.remove(player.getGuid());
		}
	}
	
	public void addPlayer(Player player){
		if(!players.containsKey(player.getGuid())){
			players.put(player.getGuid(), player);
		}
	}
}
